/**
 *  test_mailbox.c - Test codes for mail box and its functions.
 *               It runs on Windows or Linux.
 *
 *  Copyright (C) 2008  ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include "test_mailbox.h"
#include "mailbox_stubs.h"
#include "kernel/mailbox.c"


static void test_mboxClear_0(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Tested function call */
    mboxClear(_pMail);
}



static void test_mboxClear_1(void)
{
    /* Initializing argument 1 (pMail) */
    struct mailbox _pMail_0 = {0};
    mail_t *_pMail = &_pMail_0;

    /* Tested function call */
    mboxClear(_pMail);
}



static void test_mboxDel_0(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Tested function call */
    mboxDel(_pMail);
}



static void test_mboxDel_1(void)
{
    /* Initializing argument 1 (pMail) */
    struct mailbox _pMail_0 = {0};
    mail_t *_pMail = &_pMail_0;

    /* Tested function call */
    mboxDel(_pMail);
}



static void test_mboxInit_0(void)
{
    /* Initializing argument 1 (Size) */
    uword_t _Size = 0;

    /* Tested function call */
    mail_t *_result = mboxInit(_Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxInit_1(void)
{
    /* Initializing argument 1 (Size) */
    uword_t _Size = 1;

    /* Tested function call */
    mail_t *_result = mboxInit(_Size);
    CU_ASSERT_PTR_NOT_NULL(_result);

}


static void test_mboxInit_2(void)
{
    /* Initializing argument 1 (Size) */
    uword_t _Size = 16;

    /* Tested function call */
    mail_t *_result = mboxInit(_Size);
    CU_ASSERT_PTR_NOT_NULL(_result);
}



static void test_mboxInit_3(void)
{
    /* Initializing argument 1 (Size) */
    uword_t _Size = ~0;

    /* Tested function call */
    mail_t *_result = mboxInit(_Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxInit_4(void)
{
    /* Initializing argument 1 (Size) */
    uword_t _Size = 1u;

    /* Tested function call */
    mail_t *_result = mboxInit(_Size);
    CU_ASSERT_PTR_NOT_NULL(_result);
}



static void test_mboxInit_5(void)
{
    /* Initializing argument 1 (Size) */
    uword_t _Size = 0u;

    /* Tested function call */
    mail_t *_result = mboxInit(_Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxGetUsed_0(void) {
    /* Initializing argument 1 (pBuf) */
    const mail_t *_pMail = 0;

    /* Tested function call */
    word_t _result = mboxGetUsed(_pMail);
    CU_ASSERT_EQUAL(_result, -1);
}



static void test_mboxGetUsed_1(void) {
    /* Initializing argument 1 (pBuf) */
    struct mailbox _pMail_0 = {0};
    const mail_t *_pMail = &_pMail_0;

    word_t _result = mboxGetUsed(_pMail);
    CU_ASSERT_EQUAL(_result, -1);
}



static void test_mboxRev_0(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = 0;

    /* Tested function call */
    void *_result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxRev_1(void)
{
    /* Initializing argument 1 (pMail) */
    struct mailbox _pMail_0 = {0};
    mail_t *_pMail = &_pMail_0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = 0;

    /* Tested function call */
    void *_result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxRev_2(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = 0;

    /* Tested function call */
    void *_result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);

}



static void test_mboxRev_3(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = 1;

    /* Tested function call */
    void *_result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxRev_4(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = -1;

    /* Tested function call */
    void * _result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxRev_5(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = 0;

    /* Tested function call */
    void * _result  = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxRev_6(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = ~0;

    /* Tested function call */
    void *_result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxRev_7(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = 8;

    /* Tested function call */
    void *_result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxRev_8(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = 2;

    /* Tested function call */
    void *_result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxRev_9(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (SendID) */
    word_t _SendID = 1;

    /* Tested function call */
    void *_result = mboxRev(_pMail, _Key, _SendID);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_mboxSend_0(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = 0;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_0 = "";
    void *_pMsg = (void*)_pMsg_0;

    /* Tested function call */
    uword_t _result  = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_1(void)
{
    /* Initializing argument 1 (pMail) */
    struct mailbox _pMail_0 = {0};
    mail_t *_pMail = &_pMail_0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = 0;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_1 = "";
    void *_pMsg = (void *)_pMsg_1;

    /* Tested function call */
    uword_t _result = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_2(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail  = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = ~0;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = 0;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_0 = 0;
    void *_pMsg = (void*)_pMsg_0;

    /* Tested function call */
    uword_t _result = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_3(void)
{
    /* Initializing argument 1 (pMail) */
    struct mailbox _pMail_0 = {0};
    mail_t *_pMail = &_pMail_0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0u;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = 0;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_1 = 0;
    void *_pMsg = (void*)_pMsg_1;

    /* Tested function call */
    uword_t _result = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_4(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 4;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = -1;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_0 = "";
    void *_pMsg = (void*)_pMsg_0;

    /* Tested function call */
    uword_t _result = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_5(void)
{
    /* Initializing argument 1 (pMail) */
    struct mailbox _pMail_0 = {0};
    mail_t *_pMail = &_pMail_0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 1u;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = 1;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_1 = "";
    void *_pMsg = (void*)_pMsg_1;

    /* Tested function call */
    uword_t _result  = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_6(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 1;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = 1;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_0 = 0;
    void *_pMsg = (void*)_pMsg_0;

    /* Tested function call */
    uword_t _result = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_7(void)
{
    /* Initializing argument 1 (pMail) */
    struct mailbox _pMail_0 = {0};
    mail_t *_pMail = &_pMail_0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = ~0;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = 4;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_1 = 0;
    void *_pMsg  = (void*)_pMsg_1;

    /* Tested function call */
    uword_t _result  = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_8(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = 0;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = 0;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_0 = "";
    void *_pMsg = (void*)_pMsg_0;

    /* Tested function call */
    uword_t _result  = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_mboxSend_9(void)
{
    /* Initializing argument 1 (pMail) */
    mail_t *_pMail = 0;

    /* Initializing argument 2 (Key) */
    uword_t _Key = ~0;

    /* Initializing argument 3 (RevID) */
    word_t _RevID = ~0;

    /* Initializing argument 4 (pMsg) */
    char *_pMsg_0 = "";
    void *_pMsg = (void*)_pMsg_0;

    /* Tested function call */
    uword_t _result = mboxSend(_pMail, _Key, _RevID, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



#define MBOX_SIZE 8
#define SEND_ID 100
#define REV_ID 101

static void test_mailbox_uniting(void) {
    word_t i;
    char_t *str = NULL;
    mail_t *mbox = ((void*)0);
    char msg[][10] = {"Message 0", "Message 1", "Message 2", "Message 3",
                      "Message 4","Message 5", "Message 6", "Message 7"};

    mbox = mboxInit(MBOX_SIZE);
    CU_ASSERT_PTR_NOT_NULL(mbox);

    set_id(SEND_ID);
    CU_ASSERT_EQUAL(current()->id, SEND_ID);

    for(i=0; i<MBOX_SIZE; i++) {
        mboxSend(mbox, i, REV_ID, msg[i]);
    }
    CU_ASSERT_EQUAL(mboxGetUsed(mbox), MBOX_SIZE);

    set_id(REV_ID);
    CU_ASSERT_EQUAL(current()->id, REV_ID);
    for(i=0; i<MBOX_SIZE; i++) {
        str = (char*)mboxRev(mbox, i, SEND_ID);
        CU_ASSERT_PTR_EQUAL(str, msg[i]);
    }
    CU_ASSERT_EQUAL(mboxGetUsed(mbox), 0);

    for(i=0; i<MBOX_SIZE; i++) {
        mboxSend(mbox, i, REV_ID, msg[i]);
    }
    mboxClear(mbox);
    CU_ASSERT_EQUAL(mboxGetUsed(mbox), 0);

    mboxDel(mbox);
}



int test_mailbox_init(void) {
    printf("Mail box test ...\n\n");
    return 0;
}



int test_mailbox_clean(void) {
    printf("Mail box test is over!\n\n");
    return 0;
}



CU_TestInfo test_mailbox_cases[] = {
    {"test_mboxClear_0", test_mboxClear_0},
    {"test_mboxClear_1", test_mboxClear_1},
    {"test_mboxDel_0", test_mboxDel_0},
    {"test_mboxDel_1", test_mboxDel_1},
    {"test_mboxInit_0", test_mboxInit_0},
    {"test_mboxInit_1", test_mboxInit_1},
    {"test_mboxInit_2", test_mboxInit_2},
    {"test_mboxInit_3", test_mboxInit_3},
    {"test_mboxInit_4", test_mboxInit_4},
    {"test_mboxInit_5", test_mboxInit_5},
    {"test_mboxGetUsed_0", test_mboxGetUsed_0},
    {"test_mboxGetUsed_1", test_mboxGetUsed_1},
    {"test_mboxRev_0", test_mboxRev_0},
    {"test_mboxRev_1", test_mboxRev_1},
    {"test_mboxRev_2", test_mboxRev_2},
    {"test_mboxRev_3", test_mboxRev_3},
    {"test_mboxRev_4", test_mboxRev_4},
    {"test_mboxRev_5", test_mboxRev_5},
    {"test_mboxRev_6", test_mboxRev_6},
    {"test_mboxRev_7", test_mboxRev_7},
    {"test_mboxRev_8", test_mboxRev_8},
    {"test_mboxRev_9", test_mboxRev_9},
    {"test_mboxSend_0", test_mboxSend_0},
    {"test_mboxSend_1", test_mboxSend_1},
    {"test_mboxSend_2", test_mboxSend_2},
    {"test_mboxSend_3", test_mboxSend_3},
    {"test_mboxSend_4", test_mboxSend_4},
    {"test_mboxSend_5", test_mboxSend_5},
    {"test_mboxSend_6", test_mboxSend_6},
    {"test_mboxSend_7", test_mboxSend_7},
    {"test_mboxSend_8", test_mboxSend_8},
    {"test_mboxSend_9", test_mboxSend_9},
    {"test_mailbox_uniting", test_mailbox_uniting},
    CU_TEST_INFO_NULL
};
